package com.wechat.wins.handler;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.Enumeration;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public final class RestContollerFactroy {
    private final static Logger logger = LoggerFactory.getLogger(RestContollerFactroy.class);

    public void registerController(final String packageName) {
        final String pkgDir = packageName.replace('.', '/');
        try {
            final Enumeration<URL> urls = ClassLoader.getSystemClassLoader().getResources(pkgDir);
            while(urls.hasMoreElements()) {
                final URL url = urls.nextElement();
                final String protocol = url.getProtocol();
                if ("file".equalsIgnoreCase(protocol)) {
                    final String filePath = URLDecoder.decode(url.getFile(), "UTF-8");
                    final File dir = new File(filePath);
                    if (!dir.exists()) {
                        return;
                    }
                    final File[] files = dir.listFiles(filter -> filter.getName().endsWith("class"));
                    if (files != null && files.length > 0) {
                        for (final File f : files) {
                            String className = f.getName();
                            className = packageName + '.' + className.substring(0, className.length() - 6);
                            final Class<?> clazz = Class.forName(className);
                            registerClass(clazz);
                        }
                    }

                }
            }
        } catch (final ClassNotFoundException e) {
            logger.error("Class not found", e);
//            e.printStackTrace();
        } catch (final IOException e) {
            logger.error("Package not found", e);
            e.printStackTrace();
        }
    }

    private void registerClass(final Class<?> clazz) {
//        final String className = clazz.getName();
//        logger.info("Registered REST Controller: {}", className);
//        final ControllerBean bean = new ControllerBean(clazz, clazz.getAnnotation(Controller.class).singleton());
//        ControllerMappingRegistry.registerBean(className, bean);
//
//        String url = null;
//        final RequestMapping requestMapping = clazz.getAnnotation(RequestMapping.class);
//        if (requestMapping != null) {
//            url = requestMapping.value();
//        }
//        final Method[] methods = clazz.getMethods();
//        for (final Method method : methods) {
//            RequestHandler handler = null;
//            // 遍历所有method，生成ControllerMapping并注册。
//            if(method.getAnnotation(GetMapping.class) != null) {
//                handler = new GetMappingRegisterStrategy();
//            } else if(method.getAnnotation(PostMapping.class) != null) {
//                handler = new PostMappingRegisterStrategy();
//            } else if(method.getAnnotation(PutMapping.class) != null) {
//                handler = new PutMappingRegisterStrategy();
//            } else if(method.getAnnotation(DeleteMapping.class) != null) {
//                handler = new DeleteMappingRegisterStrategy();
//            } else if(method.getAnnotation(PatchMapping.class) != null) {
//                handler = new PatchMappingRegisterStrategy();
//            }
//
//            if(handler != null) {
//                final RequestMappingHandlerContext mappingHandlerCtx = new RequestMappingHandlerContext(handler);
//                mappingHandlerCtx.registerMapping(clazz, url, method);
//            }
//        }
    }
}
